package com.world.blog.service.impl;

import java.util.List;

import com.world.blog.domain.BlogTag;
import com.world.common.utils.AssertUtil;
import com.world.common.utils.DateUtils;
import com.world.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.world.blog.mapper.TagMapper;
import com.world.blog.service.IBlogTagService;

/**
 * 标签Service业务层处理
 * 
 * @author LiJunXi
 * @date 2025-05-19
 */
@Service
public class BlogTagServiceImpl implements IBlogTagService
{
    @Autowired
    private TagMapper tagMapper;

    /**
     * 查询标签
     * 
     * @param id 标签主键
     * @return 标签
     */
    @Override
    public BlogTag selectTagById(Long id)
    {
        return tagMapper.selectTagById(id);
    }

    /**
     * 查询标签列表
     * 
     * @param blogTag 标签
     * @return 标签
     */
    @Override
    public List<BlogTag> selectTagList(BlogTag blogTag)
    {
        if (!SecurityUtils.isAnonymous() && !SecurityUtils.isBlogAdmin() && !SecurityUtils.isAdmin()) {
            blogTag.setUserId(SecurityUtils.getUserId());
        }
        return tagMapper.selectTagList(blogTag);
    }

    /**
     * 新增标签
     * 
     * @param blogTag 标签
     * @return 结果
     */
    @Override
    public int insertTag(BlogTag blogTag)
    {
        blogTag.checkParams();
        blogTag.setUserId(SecurityUtils.getUserId());
        blogTag.setCreateTime(DateUtils.getNowDate());
        return tagMapper.insertTag(blogTag);
    }

    /**
     * 修改标签
     * 
     * @param blogTag 标签
     * @return 结果
     */
    @Override
    public int updateTag(BlogTag blogTag)
    {
        blogTag.checkParams();
        BlogTag find = tagMapper.selectTagById(blogTag.getId());
        AssertUtil.isNotEmpty(find, "标签不存在");
        AssertUtil.isTrue(SecurityUtils.getUserId().equals(find.getUserId()), "权限不足");
        find.setName(blogTag.getName());
        find.setUpdateTime(DateUtils.getNowDate());
        return tagMapper.updateTag(find);
    }

    /**
     * 批量删除标签
     * 
     * @param ids 需要删除的标签主键
     * @return 结果
     */
    @Override
    public int deleteTagByIds(Long[] ids)
    {
        return tagMapper.deleteTagByIds(ids);
    }

    /**
     * 删除标签信息
     * 
     * @param id 标签主键
     * @return 结果
     */
    @Override
    public int deleteTagById(Long id)
    {
        return tagMapper.deleteTagById(id);
    }
}
